/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : home.js                                 
*  Description  : 클럽개별메인 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-06-20  이윤노    Created.  
*  2012-06-00  이윤노    Complete.  
*******************************************************************************/

jQuery(function($){
	
	function clsTitle(){
		this._HTMLDom = null;
		this.errMsg = null;
		this._ERRMSG = {
			"_NOVALUE": "제목을 입력하여 주세요."	
		}
		this._init();	
	}
	
	clsTitle.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.club_creat table tbody tr:eq(0) td input:text");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.attr("maxlength", "30");
		}	
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);	
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();	
		}
		, focus: function(){
			this._HTMLDom["$layer"].focus();	
		}
		, isValid: function(){
			if(this.getValue()=="") {
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;	
			}
			return true;
		}
		, getError: function(){
			return this.errMsg;	
		}
	}
	
	function clsPhone(){
		this._HTMLDom = null;
		this.errMsg = null;
		this._ERRMSG = {
			"_NOVALUE": "휴대폰 번호를 입력해 주십시요."
			, "_INVALID": "정상적인 휴대폰 번호가 아닙니다."
		}
		this._init();	
		
	}
	
	clsPhone.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.club_creat table tbody tr:eq(1) td input:text");
			this._HTMLDom = {
				"$layer": $layer
			}	
			
			$layer.eq(0).attr("maxlength",3);
			$layer.eq(1).attr("maxlength",4);
			$layer.eq(2).attr("maxlength",4);
		}	
		, setValue: function(val){
			this._HTMLDom["$layer"].eq(0).val(val.substr(0,3));	
			this._HTMLDom["$layer"].eq(1).val(val.substr(3,4));	
			this._HTMLDom["$layer"].eq(2).val(val.substr(7,4));				
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			
			return $layer.eq(0).val() + $layer.eq(1).val() + $layer.eq(2).val();	
		}
		, focus: function(){
			this._HTMLDom["$layer"].focus();	
		}		
		, isValid: function(){
			var $layer = this._HTMLDom["$layer"];
			
			var val1 = $layer.eq(0).val();
			var val2 = $layer.eq(1).val();
			var val3 = $layer.eq(2).val();
			
			var reg1 = /^[0-9]{3}$/i;
			var reg2 = /^[0-9]{3,4}$/i;
			var reg3 = /^[0-9]{4,4}$/i;
			
			this.errMsg = null;
			this.$errObj = null;
			
			if(val1.length == 0 || val2.length == 0 || val3.length == 0){
				this.errMsg = this._ERRMSG["_NOVALUE"];				
				if(val1.length == 0) this.$errObj = $layer.find("input:text:eq(0)");
				else if(val2.length == 0)  this.$errObj = $layer.find("input:text:eq(1)");
				else if(val3.length == 0)  this.$errObj = $layer.find("input:text:eq(2)");
				return false;
			}else if(!reg1.test(val1)){
				this.errMsg = this._ERRMSG["_INVALID"];
				return false;
			}else if(!reg2.test(val2)){
				this.errMsg = this._ERRMSG["_INVALID"];				
				return false;
			}else if(!reg3.test(val3)){
				this.errMsg = this._ERRMSG["_INVALID"];				
				return false;
			}
			return true;
		}
		, getError: function(){
			return this.errMsg;	
		}
	}
	
	function clsEmail(){
		this._HTMLDom = null;
		this.listData = null;		
		this.errMsg = null;
		this._ERRMSG = {
			"_NOVALUE": "이메일을 입력하여 주세요."	
			, "_NOCOM": "메일공급자를 입력하여 주세요."
			, "_NOREG": "정상적인 이메일 주소가 아닙니다."
		}
		this._init();	
	}
	
	clsEmail.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.club_creat table tbody tr:eq(2) td");
			this._HTMLDom = {
				"$layer": $layer.find("input:text")
				, "$select": $layer.find("select")
			}
			
			var tmp = [{
				"이메일": ""
				, "텍스트": "직접입력"
			}].concat(_gf.getEmailList());
			
//			var tmp = [{
//				"주소": ""
//				, "공급자": "직접입력"
//			}
//			, {
//				"주소": "daum.net"
//				, "공급자": "다음"
//			}
//			, {
//				"주소": "naver.com"
//				, "공급자": "네이버"
//			}
//			, {
//				"주소": "lycos.com"
//				, "공급자": "라이코스"
//			}
//			, {
//				"주소": "msn.com"
//				, "공급자": "MSN"
//			}
//			, {
//				"주소": "empas.com"
//				, "공급자": "엠파스"
//			}
//			, {
//				"주소": "minigate.net"
//				, "공급자": "미니게이트"
//			}]	
			this.setMailbox(tmp)
		}	
		, setMailbox: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $select = this._HTMLDom["$select"];
			var thisClass = this;
			
			$select.empty();
			
			for(var i=0;i<this.listData.length;++i){
				$select.append("<option>"+this.listData[i]["텍스트"]+"</option>");	
			}
			
			$select.change(function(event){				
				var idx = $select.find("option").index($select.find(":selected"));
				
				$layer.eq(1).val(thisClass.listData[idx]["이메일"]);
			});
		}
		, setValue: function(val){
			var email = val.split("@")
			this._HTMLDom["$layer"].eq(0).val(email[0]);
			this._HTMLDom["$layer"].eq(1).val(email[1]);
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			return $layer.eq(0).val() +'@'+ $layer.eq(1).val();	
		}
		, focus: function(){
			this._HTMLDom["$layer"].focus();	
		}		
		, isValid: function(){
			
			var val1 = this._HTMLDom["$layer"].eq(0).val();
			var val2 = this._HTMLDom["$layer"].eq(1).val();
			
			var reg1 = /[A-Za-z0-9]$/ig;
			var reg2 = /[A-Za-z0-9]$/i;
			
			if(val1=="") {
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;	
			}else if(val2 == ""){
				this.errMsg = this._ERRMSG["_NOCOM"];
				return false;	
			}else if(!reg1.test(val1)){
				this.errMsg = this._ERRMSG["_NOREG"];
				return false;	
			}else if(!reg2.test(val2)){
				this.errMsg = this._ERRMSG["_NOREG"];
				return false;	
			}
			return true;
		}
		, getError: function(){
			return this.errMsg;	
		}
	}
	
	function clsFile(){
		this._HTMLDom = null;	
		this._FILE = null;
		this._init();
	}
	
	clsFile.prototype = {
		_init: function(){
			var thisClass = this;
			var $layer = $("div#container section#main_content div.club_creat table tbody tr:eq(3) td");
			this._HTMLDom = {
				"$layer": $layer.find("input:text")	
				, "$imgBtn": $layer.find("a>img[alt=파일첨부]")
			}
			$layer.find("input:text").prop("readonly",true);
			
			var p = this.$getBtnSubmit().position();
			_gf.setUploadFileImage(this.$getBtnSubmit(), null, function(ret){
				thisClass.destroy(ret["파일리스트"][0]);
			}, p);
		}	
		, getValue: function(){
			if(!this._FILE){
				return "";	
			}
			return this._FILE["파일키"] +"," + this._FILE["섬네일"] +","+ this._FILE["파일명"] + "," + this._FILE["원본파일명"];
		}
		, setValue: function(thisData){
			this._FILE = thisData;	
		}
		, destroy: function(ret){
			var $layer = this._HTMLDom["$layer"];
			
			this._FILE = ret;
			this._FILE["파일키"] = "NOT";
			this._FILE["섬네일"] = "NOT";
			
			$layer.val(_gf.filePath2Url(ret["파일명"]));
						
			_gf.spinEnd();	
		}
		, $getBtnSubmit: function(){
			return this._HTMLDom["$imgBtn"];
		}
	}
	
	function clsContents(){
		this._HTMLDom = null;
		this.errMsg = null;
		this._ERRMSG = {
			"_NOVALUE": "내용을 입력하여 주세요."	
		}
		this._init();	
	}
	
	clsContents.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.club_creat table tbody tr:eq(4) td textarea");
			this._HTMLDom = {
				"$layer": $layer
			}	
			$layer.attr("maxlength", "1000");
		}	
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();	
		}
		, focus: function(){
			this._HTMLDom["$layer"].focus();	
		}		
		, isValid: function(){
			if(this.getValue()=="") {
				this.errMsg = this._ERRMSG["_NOVALUE"];
				return false;	
			}
			return true;
		}
		, getError: function(){
			return this.errMsg;	
		}
	}
	
	function clsComm()
	{
				
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){		
			this.faqListRequest = {
				"카테고리키": null	
			}
		}
//------------------------ FAQ 카테고리------------------------//	
		, getFaqCategory: function(){
			var thisClass = this;
	
			var szServicename = "getFaqCateList";
			
			// ajax 시작
			_gf.ajax({
				"sid":"QA001",
				"servicename": szServicename
			}, {
								
			}, function(ret){
				// callback and mapping
				thisClass.faqCategory = [];
									
				if(ret["msp"]["contents"]["items"]){					
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"]	= new Array(ret["msp"]["contents"]["items"]);
					}					
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){													
						var obj = ret["msp"]["contents"]["items"][i];
												
						thisClass.faqCategory.push({
							"카테고리키": _util.nvl(obj["faq__detail__type"])
							, "카테고리명": _util.nvl(obj["faq__detail__name"])
						});
					}
				}
				
				$(thisClass).trigger("evtFinishFaqCategory" );
			});
		}
		, getFaqCategoryData: function(){
			return this.faqCategory;
		}
//------------------------ FAQ 카테고리------------------------//		
//------------------------ 제휴문의등록------------------------//		
		, InsertFaq: function(arg){
			var thisClass = this;
			var szTitle = arg["제목"];
			var szPhone = arg["핸드폰"];
			var szEmail = arg["이메일"];
			var szContents = arg["내용"];
			var szFile = arg["첨부파일"];
	
			// ajax 시작			
			//parameter setting and run
			_gf.ajax({
				"sid":"MB001",
				"servicename":"regSuggest"
			}, {
				"phone": szPhone
				, "email": szEmail
				, "title": szTitle
				, "contents": szContents
				, "file_list": szFile				
			}, function(ret){
				// callback and mapping
				var resData = {
					"응답코드": (ret["msp"]["contents"]["serviceResult"] == "00000"?true:false)
					, "메세지": _gf.getResultMsg(ret)
				}
				
				$(thisClass).trigger("evtFinishFaqSubmit", [resData]);
			});
			// ajax 끝
		}
//------------------------ 제휴문의등록------------------------//		
	}
	
	function clsControl()
	{
		this.objComm = null;
		this.objTitle = null;
		this.objPhone = null;
		this.objEmail = null;
		this.objContents = null;
		this.objFile = null;
	}
	
	clsControl.prototype = {
		_init: function(){
					
			this.objComm = new clsComm();
			this.objTitle = new clsTitle();
			this.objPhone = new clsPhone();
			this.objEmail = new clsEmail();
			this.objContents = new clsContents();
			this.objFile = new clsFile();
			
			this._bind();
		}
		, _bind: function(){
			var thisClass = this;
			
			//제휴문의 등록 완료 이벤트
			$(this.objComm).bind("evtFinishFaqSubmit", function(event, resData){
				_gf.spinEnd();
				alert(resData["메세지"]);
				if(resData["응답코드"]){
					document.location.href = "partner.html";	
				}
			});
			//확인
			$("p.btn_center a img[alt=확인]").click(function(){
				
				if(!thisClass.objTitle.isValid()){
					alert(thisClass.objTitle.getError());
					return;
				}else if(!thisClass.objPhone.isValid()){
					alert(thisClass.objPhone.getError());
					return;
				}else if(!thisClass.objEmail.isValid()){
					alert(thisClass.objEmail.getError());
					return;
				}else if(!thisClass.objContents.isValid()){
					alert(thisClass.objContents.getError());
					return;
				}	
				
				var submitData = {
					"제목": thisClass.objTitle.getValue()
					, "핸드폰": thisClass.objPhone.getValue()
					, "이메일": thisClass.objEmail.getValue()
					, "내용": thisClass.objContents.getValue()
					, "첨부파일": thisClass.objFile.getValue()
				}
				
				_gf.spinStart();
				thisClass.objComm.InsertFaq(submitData);
			});
			//취소
			$("p.btn_center a img[alt=취소]").click(function(){
				document.location.href = "customer.html";				
			});
		
		}	
		, tmp: function(){
			this.objTitle.setValue("제목");
			this.objPhone.setValue("01029059139");
			this.objEmail.setValue("dldbssh84@gmail.com");
			this.objContents.setValue("냉무");
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});	